package com.googlecode.horncomb.orm.ibatis;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Set;

import org.springframework.dao.DataAccessException;

import com.googlecode.horncomb.bean.order.Order;
import com.googlecode.horncomb.bean.paged.PagedList;

/**
 * SqlMap entity DAO implement. <br/>
 * @author Daniel
 */
@SuppressWarnings("unchecked")
public class DefaultCommonSqlMapEntityDao extends
		AbstractCommonSqlMapEntityDao implements CommonSqlMapEntityDao {
	private static final long serialVersionUID = 2383392591630044311L;

	/**
	 * default constructor
	 */
	public DefaultCommonSqlMapEntityDao() {
		super();
	}

	// ----------------------------------------------------------------------
	/**
	 * {@inheritDoc}
	 * @see com.googlecode.horncomb.orm.dao.EntityBaseDao#getById(java.io.Serializable)
	 * @see #getById(Serializable, String)
	 */
	public Object getById(Serializable id) throws DataAccessException {
		return this.getById(id, this.getStatementName);
	}

	/**
	 * {@inheritDoc}
	 * @see com.googlecode.horncomb.orm.dao.EntityBaseDao#insert(java.io.Serializable)
	 * @see #insert(Serializable, String)
	 */
	public void insert(Object entity) throws DataAccessException {
		this.insert(entity, this.insertStatementName);
	}

	/**
	 * {@inheritDoc}
	 * @see cn.horncomb.orm.dao.EntityBaseDao#update(java.io.Serializable)
	 */
	public void update(Object entity) throws DataAccessException {
		this.update(entity, this.updateStatementName);
	}

	/**
	 * {@inheritDoc}
	 * @see com.googlecode.horncomb.orm.dao.EntityBaseDao#deleteById(java.io.Serializable)
	 * @see #deleteById(Serializable, String)
	 */
	public void deleteById(Serializable id) throws DataAccessException {
		this.deleteById(id, this.deleteStatementName);
	}

	/**
	 * {@inheritDoc}
	 * @see com.googlecode.horncomb.orm.dao.EntityBaseDao#deleteByIds(Set)
	 * @see #deleteByIds(Set, String);
	 */
	public void deleteByIds(Set ids) throws DataAccessException {
		this.deleteByIdSet(ids, this.deleteStatementName);
	}

	/**
	 * {@inheritDoc}
	 * @see cn.horncomb.orm.dao.EntityBaseDao#save(java.io.Serializable)
	 */
	public void save(Object entity) throws DataAccessException {
		if (this.logger.isDebugEnabled()) {
			this.logger.debug("save entity: " + entity);
		}
		if (this.isNew(entity)) {
			this.insert(entity);
		} else {
			this.update(entity);
		}
	}

	/**
	 * {@inheritDoc}
	 * @see com.googlecode.horncomb.orm.ibatis.SqlMapEntityDao#queryByExample(java.io.Serializable,
	 *      java.util.Collection, int, int)
	 */
	public PagedList queryByExample(Object example, Order order, int page,
			int pageSize) {
		return this.query(SqlMapEntityDaoUtil
				.convertExampleToConstraints(example), order, page, pageSize);
	}

	/**
	 * {@inheritDoc}
	 * @see com.googlecode.horncomb.orm.ibatis.SqlMapEntityDao#queryCountByExample(java.io.Serializable)
	 */
	public int queryCountByExample(Object example) {
		return this.queryCount(SqlMapEntityDaoUtil
				.convertExampleToConstraints(example));
	}

	/**
	 * {@inheritDoc}
	 */
	public Collection queryByExample(Object example) {
		return this.query(SqlMapEntityDaoUtil
				.convertExampleToConstraints(example));
	}

	/**
	 * {@inheritDoc}
	 * @see com.googlecode.horncomb.orm.ibatis.SqlMapEntityDao#queryByExample(java.io.Serializable,
	 *      java.util.Collection)
	 */
	public List queryByExample(Object example, Order order)
			throws DataAccessException {
		return this.query(SqlMapEntityDaoUtil
				.convertExampleToConstraints(example), order);
	}

	public Collection queryAll() {
		return this.query(null, null);
	}

	public List queryAll(Order order) {
		return this.query(null, order);
	}

	public PagedList queryAll(Order order, int page, int pageSize) {
		return this.query(null, order, page, pageSize);
	}

}
